package hz.mobileproject.pda.toolsmanager.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.blankj.utilcode.util.SPUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;

import java.util.ArrayList;
import java.util.List;

import hz.mobileproject.pda.R;
import hz.mobileproject.pda.listener.CustomNetCallBack;
import hz.mobileproject.pda.network.Constance;
import hz.mobileproject.pda.toolsmanager.adapter.WaiJieAndGuiHuanAdapter;
import hz.mobileproject.pda.toolsmanager.bean.request.RQ_TOOL_BORROW;
import hz.mobileproject.pda.toolsmanager.bean.response.RP_WaiJie_GuiHuan;
import hz.mobileproject.pda.utils.HttpRqUtils;

/**
 * Created by Alex on 2018/5/9.
 */

public class GuiHuanJiLuFragment extends Fragment {


    private SmartRefreshLayout mSmartRefreshLayout;
    private ListView lv_list;
    private int mPage = 1;
    private SPUtils spUtils;
    private Gson mGson;
    private List<RP_WaiJie_GuiHuan> mLists;
    private WaiJieAndGuiHuanAdapter mAdapter;
    private Context mContext;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = View.inflate(container.getContext(), R.layout.ft_fafang,null);
        mSmartRefreshLayout = (SmartRefreshLayout) v.findViewById(R.id.smartLayout);
        lv_list = (ListView) v.findViewById(R.id.lv_list);
        return v;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mPage = 1;
        mContext = getActivity();
        spUtils = SPUtils.getInstance("app_login");
        mGson = new Gson();
        mAdapter = new WaiJieAndGuiHuanAdapter(mContext, new ArrayList<RP_WaiJie_GuiHuan>(), false);
        lv_list.setAdapter(mAdapter);
        mSmartRefreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                mSmartRefreshLayout.finishLoadmore();
                getList(mPage);

            }
        });


        getList(mPage);

    }

    private void getList(int mPage) {
        RQ_TOOL_BORROW q = new RQ_TOOL_BORROW();
        q.cus_number = spUtils.getString("cusnumber");
        q.dept_id = spUtils.getString("department_id");
        q.status = "2";
        q.page = mPage + "";
        HttpRqUtils.httpPost(Constance.BASE_URL + Constance.URL_TOOL_WAIJIE_AND_GUIHUAN, q, callback);
    }


    private CustomNetCallBack callback = new CustomNetCallBack() {
        @Override
        public void onSuccessFromSer(int rtnCode, String rtnMsg, String data) {

            mLists = mGson.fromJson(data,new TypeToken<List<RP_WaiJie_GuiHuan>>(){}.getType());
            mAdapter.clearAdd(mLists);
            if(mLists == null || mLists.size() == 0) {
//                ToastUtils.showLong("没有更多数据了");
                return;
            }
            mPage++;
        }


        @Override
        public void onErrorFromSer(int rtnCode, String rtnMsg) {
            mSmartRefreshLayout.finishLoadmore();
        }

        @Override
        public void onErrorFromNet(Throwable ex, boolean isOnCallback) {
            mSmartRefreshLayout.finishLoadmore();
        }
    };
}
